﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pixels.Logic
{
    public class Map : IEnumerable<Tile> {
        private Tile[,] _tiles;

        public int Width {
            get {
                if (_tiles == null) return -1;
                return _tiles.GetLength(0);
            }
        }

        public int Height {
            get {
                if (_tiles == null) return -1;
                return _tiles.GetLength(1);
            }
        }

        public void create(int width, int height) {
            _tiles = new Tile[width, height];
            for (int iy = 0; iy < height; iy++)
                for (int ix = 0; ix < width; ix++) {
                    _tiles[ix, iy] = new Tile(this, ix, iy);
                }
        }

        public Tile this[int x, int y] {
            get  { 
                // warning : not particularly performant, will optimize
                if (x < 0) return null; if (x >= Width) return null;
                if (y < 0) return null; if (y >= Height) return null;
                return _tiles[x, y];
            }
        }

        IEnumerator<Tile> IEnumerable<Tile>.GetEnumerator() {
            foreach (var t in _tiles) yield return t;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return _tiles.GetEnumerator();
        }
    }
}
